$(document).ready(function(){
	windowOpenClose();
	menuAction();
});

function windowOpenClose(){
	//var window = jQuery('ul.window');
	var buttonOpen = jQuery('ul.window').children('li.title').children('a.open');
	var buttonClose = jQuery('ul.window').children('li.title').children('a.close');
	
	buttonOpen.click(function (){
		//window.children('li.content').show();
		$(this).parent().parent().find('li.content').first().show();
	});
	
	buttonClose.click(function (){
		//window.children('li.content').hide();
		$(this).parent().parent().find('li.content').first().hide();
	});
}

function menuAction(){
	jQuery('li.debug a.function').click(function(){putContentHidden(jQuery('div.debug'), jQuery('div.admin-col-l'));});
	jQuery('li.log a.function').click(function(){loadLog();});
}

function appendHtml(html, to) {
	to.append(html);
}

function putHtml(html, to) {
	clearAdminPanels();
	to.html(html);
}

function putContentHidden(from, to) {
	clearAdminPanels();
	to.html(from.html());
}

function clearAdminPanels(){
	jQuery('div.admin-col-l').html('');
	jQuery('div.admin-col-r').html('');
}

function toggleLoader(to){
	var loader = '<img class="loader" src="/inc/templates/default/img/ico/admin/circle-loader-35.png" title="wait" />';
	if (to.html().indexOf('circle-loader-35.png') > 0 ){
		to.children('img.loader').remove();
	} else {
		to.html(loader);
	}
}

function loadLog(){
	$('#admin-top-loader').show();
	jQuery.ajax({
		url:'/ajax/core/admin/getLog.php',
		success:function (response) {
			$('#admin-top-loader').hide();
			var json = eval(response);
			if (json.success) {
				putHtml(json.html, jQuery('div.admin-col-l'));
			} else {
				alert(json.error);
			}
		}
	});
}

function eventAjaxSendForm(button, action){
	button.click(function (){
		
		var frm = $(this).parents('form');
		$('#admin-top-loader').show();
		
		jQuery.ajax({
			type: "POST",
			url: action,
			data: frm.serialize(),
			success: function(response) {
				$('#admin-top-loader').hide();
				var json = eval(response);
				
				if (json.success) {
					eval(json.jscript);					
				} else {
					alert(json.error);
				}
			}
		});

	});
}
